/*
 *  Tweak.mm
 *  

 *  Created by David Ashman on 1/12/09.
 *  Copyright 2009 David Ashman. All rights reserved.
 *
 */
#include <substrate.h>
#include <UIKit/UIEvent.h>
#include <UIKit/UITouch.h>
#include <SpringBoard/SpringBoard.h>
#include <SpringBoard/SBAwayView.h>
#include <SpringBoard/SBAwayItemsView.h>
#include <SpringBoard/SBAwayController.h>
#include <SpringBoard/SBAwayModel.h>
#include <SpringBoard/SBAlertItemsController.h>
#include <Foundation/Foundation.h>
 
static Class $SBAlertItemsController = objc_getClass("SBAlertItemsController");
static NSDate* undimTime;

MSHook(void, qd_undimScreen, SBAwayController *self, SEL sel)
{
	BOOL dimmed = [self isDimmed];
	_qd_undimScreen(self, sel);

	if (dimmed)
	{
		[undimTime release];
		undimTime = [[NSDate date] retain];
	}
}

MSHook(BOOL, qd_handleMenuButtonTap, SBAwayController *self, SEL sel)
{
	BOOL dismissWhileLocked = true;
	BOOL dismissWhileUnlocked = true;
	BOOL debug = false;

	if (NSDictionary *settings = [[NSDictionary alloc] initWithContentsOfFile:[NSString stringWithFormat:@"/User/Library/Preferences/com.ashman.QuickDismiss.plist"]]) 
	{
       		[settings autorelease];

		if (NSNumber* locked = [settings objectForKey:@"DismissWhileLocked"])
			dismissWhileLocked = [locked boolValue];
	
		if (NSNumber* unlocked = [settings objectForKey:@"DismissWhileUnlocked"])
			dismissWhileUnlocked = [unlocked boolValue];
	
		if (NSNumber* b = [settings objectForKey:@"Debug"])
			debug = [b boolValue];
	}	

	BOOL shouldDismiss = ((dismissWhileLocked && [self isLocked] && ![self isDimmed] && undimTime) ||
				(dismissWhileUnlocked && ![self isLocked]));

	if (shouldDismiss)
	{
		NSDate* now = [NSDate date];
		NSTimeInterval diff = [now timeIntervalSinceDate:undimTime];
		if (diff > 1)
		{
			// lets look for an alert item
			SBAlertItemsController* ctlr = [$SBAlertItemsController sharedInstance];
			SBAlertItem* item = [ctlr visibleAlertItem];
			if (item)
			{
				NSLog(@"QD: Dismissing %@.", item);
				[item dismiss];	
				return true;
			}
			
			if ([[self awayView] hasAwayItems])
			{
				NSLog(@"QD: Dismissing away items.");
				if ([self respondsToSelector:@selector(resetAwayItems)])
					[self resetAwayItems];
				else
					[self resetAwayItemsAndMark:YES];

				return true;
			}

			if (debug)
			{
				if ([self isLocked])
				{
					NSLog(@"QD: Creating test item.");
					Class cls = objc_getClass("SBAwayItem");
					id ai = [[cls alloc] initWithTitle:@"test!" uid:0 type:0 date:[NSDate date]];
					[[self awayModel] addOtherAwayItem:ai];
					[[self awayView] showAwayItems];
				}
				else
				{
					NSLog(@"QD: Creating test alert.");
					Class cls = objc_getClass("SBCalendarAlertItem");
					item = [[cls alloc] initWithDate:[[NSDate date] timeIntervalSince1970] title:@"Test Alert" location:nil eventId:1 isAllDay:true];
					[ctlr activateAlertItem:item];	
				}
			}
		}
	}

	return _qd_handleMenuButtonTap(self, sel);
}

#define Hook(cls, sel, imp) \
        _ ## imp = MSHookMessage($ ## cls, @selector(sel), &$ ## imp)

extern "C" void QuickDismissInit() {
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

	NSString* id = [[NSBundle mainBundle] bundleIdentifier];
	if (![id isEqualToString:@"com.apple.springboard"])
		return;

	Class $SBAwayController = objc_getClass("SBAwayController");

	Hook(SBAwayController, handleMenuButtonTap, qd_handleMenuButtonTap);
	Hook(SBAwayController, undimScreen, qd_undimScreen);

	[pool release];
}
